#include <QtCore>
#include "process.h"

Process::Process( QObject *parent , const QString &address, const QString &username, const QString &password, const QString &port, const QString &localPort ) 
: QObject(parent)
, running_(false)
, address_(address)
, username_(username)
, password_(password)
, port_(port)
, localPort_(localPort)
{

}

#if defined(Q_OS_WIN)
void Process::startPlink()
{
    QString plink = QCoreApplication::applicationDirPath() + "\\plink.exe";
    Q_ASSERT(QFile::exists(plink));
    QStringList arguments;
    arguments.append(QString("\"%s\"").arg(address_));
    arguments.append("-N");
    arguments.append("-ssh");
    arguments.append("2");
    arguments.append("-P");    
    arguments.append(port_);
    arguments.append("-l");
    arguments.append(username_);
    arguments.append("-D");
    arguments.append(localPort_);

    process = new QProcess;
    process->start(plink, arguments);
    process->waitForStarted();
    // wait for read
    // judge the output, end with "Password:"
    // write password 
}
#endif

void Process::startSSH()
{
    QStringList arguments;
    arguments.append("-qTfnN");
    arguments.append("-D");
    arguments.append(localPort_);
    arguments.append(QString("%s@%s").arg(username_).arg(address_));

    process = new QProcess;
    process->start("ssh", arguments);
    process->waitForStarted();
    // wait for read
    // judge the output, end with "Password:"
    // write password
}

void Process::Kill()
{

}

bool Process::IsRunning()
{
    return running_;
}
